﻿using Krypton.Toolkit;
using Serilog;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TSNPro.Base;
using TSNPro.Communication;
using TSNPro.Communication.Sender;
using TSNPro.MyControl;
using TSNPro.Utils;

namespace TSNPro.Device
{
    public class CoommonEventArgs:EventArgs
    {
        public CoommonEventArgs(string type,double value)
        {
            DataType=type;
            Value=value;
        }

        public string DataType { get;  }

        public double Value { get;  }
    }

    public class CommonDataItem : INotifyPropertyChanged
    {
        public CommonDataItem(string name)
        {
            Name=name;
        }

        public string Name { get; set; }


        private int _displayValue;
        public int DisplayValue 
        {
            get
            {
                return _displayValue;
            }
            set
            {
                _displayValue = value;
                OnPropertyChanged(nameof(DisplayValue));
                OnPropertyChanged(nameof(FontColor));
            }
        }

        private int _setValue;
        public int SetValue 
        {
            get 
            { 
                return _setValue; 
            }
            set 
            {
                _setValue = value;
            }
        }

        private int _errorValue;
        public int ErrorValue 
        {
            get
            {
                return _errorValue;
            }
            set
            {
                _errorValue = value;
            }
        }

        public Color FontColor
        {
            get
            {
                if(Math.Abs(DisplayValue-SetValue)>ErrorValue)
                {
                    return Color.Red;
                }
                else
                {
                    return Color.Green;
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class CommonDevice : BaseDevice
    {
        public event EventHandler<CoommonEventArgs> DataReceive;
        private BaseSender _sender;

        public CommonDevice() 
        {
        
        }

        public override void SetCommunication(List<BaseCommunication> communcaition)
        {
            base.SetCommunication(communcaition);
            _sender = communcaition[0] as BaseSender;
            _sender.DataReceived += Sender_OnDataReceived;
        }

        private void Sender_OnDataReceived(object sender, byte[] e)
        {

            //throw new NotImplementedException();
        }

        #region 温控器
        public void SetBurningZoneTemperature(double value)
        {

        }

        public void SetGasificationZoneTemperature(double value)
        {

        }

        public void SetBackPressure(double value)
        { 
        
        }
        #endregion

        #region 气流量
        //载气
        public void SetCarrierGas(double value)
        {

        }

        //进口氧
        public void SetImportedOxyGen(double value)
        {

        }

        //裂解氧
        public void SetPyrolysisOxygen(double value)
        {

        }

        //氮臭氧
        public void SetNitrogenOzone(double vlaue)
        {

        }
        #endregion

        #region 环境参数设置 
        public void SetAtmosphericStandardPressure(double value)
        {

        }

        public void SetAtmosphericWorkedPressure(double value)
        {

        }

        public void SetStandardTemperature(double value)
        {

        }

        public void SetWorkedTemperature(double value)
        {

        }

        #endregion

        public static byte[] CreateCommand(byte type,int value)
        {
            List<byte> buf = new List<byte>();
            buf.Add(0x5A);
            buf.Add(type);
            buf.AddRange(GetBytes(value));
            buf.AddRange(CrcUtils.Crc16ToBytes(buf.ToArray()));
            return buf.ToArray();
        }

        public static byte[] GetBytes(int value)
        {
            List<byte> buf = new List<byte>();
            for (int i = 0; i < 4; i++)
            {
                buf.Add((byte)(value % 256));
                value /= 256;
            }
            return buf.Reverse<byte>().ToArray();
        }

        List<CommonDataItem> _dataItems=new List<CommonDataItem>();
        public List<CommonDataItem> GetCommonDataItems()
        {
            if(!_dataItems.Any())
            {
                _dataItems.Add(new CommonDataItem("燃烧段℃") {SetValue = 120});
                _dataItems.Add(new CommonDataItem("气化段℃") { SetValue = 130 });
                _dataItems.Add(new CommonDataItem("背压psig") { SetValue = 140 });
            }
            return _dataItems;
        }




        private void sendDateValue(byte type, int value)
        {
            try
            {
                byte[] cmd= CreateCommand(type, value);
                if (_sender.IsConnected())
                {
                    _sender.Send(cmd);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("串口发送数据出错", ex);
            }
        }

        public override Control GetConfigWidget()
        {
            return base.GetConfigWidget();
        }

    }
}
